/**
 * 题目要求
 * 将下面的代码使用Promise的方式改进
 * 
 * setTimeout(function a () {
 *     var a = 'hello'
 *     setTimeout(function b () {
 *         var b = 'lagou'
 *         setTimeout(function c () {
 *             var c = 'I Love You'
 *             console.log(`${a}! ${b}, ${c}`)
 *         }, 10)
 *     }, 10)
 * }, 10)
 */

const a = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            var a = 'hello'
            resolve(a)
        }, 10)
    })
}

const b = (value) => {
    return new Promise((resolve, reject) => {
       setTimeout(() => {
           var b = 'lagou'
           resolve(`${value}! ${b}`)
       }, 10)
    })
}

const c = (value) => {
    return new Promise((resolve, reject) => {
       setTimeout(() => {
           var c = 'I Love You'
           console.log(`${value}, ${c}`)
           resolve(`${value}, ${c}`)
       }, 10)
    })
}

//第一种 纯promise写法
/* a().then(res => {
    return b(res)
}).then(res => {
    return c(res)
}) */


//第二种 promise + generator写法
/* function *main () {
    try {
        const res = yield a()
        const res1 = yield b(res)
        yield c(res1)
    } catch (error) {
        console.log(error)
    }
}
const g = main()
let result = g.next()
result.value.then(data => {
    let result1 = g.next(data)
    if (result1.done) return
    result1.value.then(data => {
        g.next(data)
    })
}) */

//第三种 async await
async function fn () {
    try {
        const res = await a()
        const res1 = await b(res)
        await c(res1)
    } catch (error) {
        console.log(error)
    }
}
fn()
